class Solution {

     public String tree2str(TreeNode root) {

        StringBuilder stringBuilder = new StringBuilder();

        tree2strChild(root,stringBuilder);

        return stringBuilder.toString();

    }



    private void tree2strChild(TreeNode t,StringBuilder stringBuilder) {

        if(t == null) {

            return ;

        }

        stringBuilder.append(t.val);

        if(t.left != null) {

            stringBuilder.append("(");

            tree2strChild(t.left,stringBuilder);

            stringBuilder.append(")");

        }else {

            if(t.right == null) {

                return;

            }else {

                stringBuilder.append("()");

            }

        }

        //判断右树

        if(t.right != null) {

            stringBuilder.append("(");

            tree2strChild(t.right,stringBuilder);

            stringBuilder.append(")");

        }else {

            return;

        }

    }